﻿using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PodcastGenerator;
using Rhino.Mocks;

namespace PodcastGeneratorTests
{
    [TestClass]
    public class CsvStoreTest
    {
        private IStore _store;
        private ISermon _sermon;
        private ISermon _secondSermon;
        private string _csvStorePath;
        
        [TestInitialize]
        public void Init()
        {
            _csvStorePath = Path.GetFullPath("CsvStore");

            _sermon = MockRepository.GenerateMock<ISermon>();
            _sermon.Stub(p => p.DownloadUri).Return(new Uri(Path.GetFullPath("TestPage\\20130106am.mp3")));
            _sermon.Stub(p => p.Date).Return(DateTime.Now);
            _sermon.Stub(p => p.Pastor).Return("The Boss");
            _sermon.Stub(p => p.Title).Return("How to train a dragon");

            _secondSermon = MockRepository.GenerateMock<ISermon>();
            _secondSermon.Stub(p => p.DownloadUri).Return(new Uri(Path.GetFullPath("TestPage\\doesntExist.mp3")));
            _secondSermon.Stub(p => p.Date).Return(DateTime.Now);
            _secondSermon.Stub(p => p.Pastor).Return("Invisible");
            _secondSermon.Stub(p => p.Title).Return("Something does not exist");

            var sermonDownloader = MockRepository.GenerateMock<ISermonDownloader>();
            sermonDownloader.Stub(d => d.ExistInStoreLocation(_sermon)).Return(true);
            _store = new CsvStore(sermonDownloader, _csvStorePath);
        }

        [TestMethod]
        public void TestPersistAll()
        {
            int savedSermonsCount = _store.PersistAll(new List<ISermon>() { _sermon});
            Assert.AreEqual(1, savedSermonsCount);
            Assert.AreEqual(2, File.ReadAllLines(_csvStorePath + "\\" + CsvStore.CsvStoreName + ".csv").Length);
        }

        [TestMethod]
        public void TestPersistNew()
        {
            int savedSermonsCount = _store.PersistAll(new List<ISermon>() { _sermon });

            var savedSermons = _store.PersistNew(new List<ISermon> {_sermon, _secondSermon});
            Assert.AreEqual(1, savedSermons.Count);
            Assert.AreEqual(3, File.ReadAllLines(_csvStorePath + "\\" + CsvStore.CsvStoreName + ".csv").Length);
        }
    }
}